import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../resource/language/generated/l10n.dart';
import '../root_page.dart';
import '../utils/constant/text_styles.dart';
import '../widget/textfield/jr_edit_text.dart';
import '../widget/textfield/jr_text_field.dart';
import '_jr_dialog_with_check.dart';
import 'dialog_data.dart';
import 'jr_dialog_with_type1.dart';
import 'jr_dialog_with_type2.dart';
import 'jr_dialog_with_type3.dart';
import 'jr_dialog_with_type4.dart';

export 'jr_dialog_user_feedback.dart';

/// UI稿：https://js.design/f/Ylx7dj?p=QrJuTkLTRK&mode=programmer
/// data:
///    TextWithHorizontalBtnDialogData 横向两个按钮的样式
///    TextWithOneBtnDialogData 单独一个按钮的样式
///    TextWithVerticalBtnDialogData 纵向两个按钮的样式
///    PicTextWithVerticalBtnDialogData 纵向两个按钮，顶部有图的样式
///
/// [backgroundBlurred] 背景是否高斯模糊
///
class JRDialog {
  static Future showCommonDialog(
      {required BuildContext context,
      required DialogData data,
      bool barrierDismissible = false,
      bool backgroundBlurred = false}) async {
    Widget? widget;
    if (data is TextWithHorizontalBtnDialogData) {
      widget = JRDialogWithType1(
        data: data,
      );
    } else if (data is TextWithOneBtnDialogData) {
      widget = JRDialogWithType2(
        data: data,
      );
    } else if (data is TextWithVerticalBtnDialogData) {
      widget = JRDialogWithType3(
        data: data,
      );
    } else if (data is PicTextWithVerticalBtnDialogData) {
      widget = JRDialogWithType4(
        data: data,
      );
    } else if (data is TextWithCheckDialogData) {
      widget = JRDialogWithCheck(
        data: data,
      );
    }
    if (widget == null) {
      return;
    }
    return showDialog(
        context: context,
        barrierDismissible: barrierDismissible,
        useRootNavigator: false,
        builder: (_) {
          Widget? finalChild;
          if (backgroundBlurred) {
            finalChild = BackdropFilter(
                filter: ImageFilter.blur(sigmaX: 8, sigmaY: 8), child: widget);
          } else {
            finalChild = widget;
          }
          return Dialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16.0.w)),
              backgroundColor: Colors.transparent,
              shadowColor: Colors.transparent,
              insetPadding: EdgeInsets.zero,
              child: finalChild);
        });
  }

  ///输入框弹窗 只有一个确定按钮
  static Future showTextFieldDialog(String title, String hintText,
      {bool barrierDismissible = true,
      List<TextInputFormatter>? inputFormatters}) {
    String result = '';
    return showDialog(
        context: rootNavigatorState.currentContext!,
        barrierDismissible: barrierDismissible,
        useRootNavigator: false,
        builder: (_) {
          return Dialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(16.0.w)),
            backgroundColor: Colors.white,
            shadowColor: Colors.transparent,
            insetPadding: EdgeInsets.symmetric(horizontal: 38.w),
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 24.w),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  SizedBox(height: 24.w),
                  Text(
                    title,
                    style: JRTextStyles.styleFont18W600
                        .copyWith(color: Color(0xFF333333)),
                  ),
                  SizedBox(height: 24.w),
                  JRTextField(
                    hintText: hintText,
                    onChanged: (str) {
                      result = str;
                    },
                    inputFormatters: inputFormatters,
                  ),
                  SizedBox(height: 20.w),
                  JRButton(
                      height: 48.w,
                      title: T.current.surer,
                      onPressed: () {
                        Get.back(result: result);
                      }),
                  SizedBox(height: 36.w),
                ],
              ),
            ),
          );
        });
  }
}
